package com.future;

/**
 * Description: 137. 只出现一次的数字 II
 * 参考图片Solution_137.png
 * 输入：nums = [2,2,3,2]
 * 输出：3
 *
 * @author weiruibai.vendor
 * Date: 2023/1/13 10:13
 */
public class Solution_137 {

    private static Solution_137 instance = new Solution_137();

    public static void main(String[] args) {
        int[] nums = new int[]{3, 5, 3, 3};
        System.out.println(instance.singleNumber(nums));
    }

    /**
     * 参考图片Solution_137.png
     *
     * @param nums
     * @return
     */
    public int singleNumber(int[] nums) {
        int N = nums.length;
        if (N < 3) {
            return nums[0];
        }
        int ans = 0;
        for (int i = 0; i < 32; i++) {
            int oneCount = 0;
            for (Integer num : nums) {
                oneCount += ((num >> i) & 1);
            }
            if (oneCount % 3 != 0) {
                ans |= (1 << i);
            }
        }
        return ans;
    }
}
